import datetime
import decimal
import json
from urllib.parse import urlparse

from flask import make_response


def response(code, msg, data):
    d = {
        "code": code,
        "msg": msg,
        "data": data
    }
    return make_response(d, code)


# def hash_password(self, password):
#     """密码加密"""
#     self.password = sha256_crypt.encrypt(password)
#
# def verify_password(self, password):
#     """校验密码"""
#     return sha256_crypt.verify(password, self.password)

def get_auth_code():
    return 9527


class JSEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, datetime.datetime):
            return o.strftime("%Y-%m-%d %H:%M:%S")
        elif isinstance(o, decimal.Decimal):
            return float(o)
        else:
            return json.JSONEncoder.default(o)

def validate_url(url):
    try:
        result = urlparse(url)
        # 检查协议和网络位置是否非空，且协议为 http/https/ftp 等
        return all([result.scheme, result.netloc]) and result.scheme in ["http", "https", "ftp"]
    except:
        return False